﻿//------------------------------------------------------------------------------
//  
//------------------------------------------------------------------------------
#region Imported Namespaces
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;

using SSRS;
using PHSRAG.Insight21.Reports;
using PHSRAG.Insight21.InsightUtilities;
using Utils = PHSRAG.Insight21.InsightUtilities;
using PHSRAG.Insight21.SearchContext;
//using UserContext = PHSRAG.Insight21.Context.UserContext;
using AgreementsConstants = PHSRAG.Insight21.InsightUtilities.Constants.AgreementsConstants;

#endregion

namespace PHSRAG.Insight21.Agreements.Reports {

    
    public class sqlContractDetailsReport : sqlReport
    {
        private const string FundNumberKey = "FundNumberKey_d6c4d45a-ec0b-4495-a9c7-ed7cb326798b";
        private const string AgreementsIdkey = "agreementIdKey_715A9505-97DF-4685-B3F6-A4B79CD16731";
 
        public sqlContractDetailsReport(string pageUrl)
            : base(pageUrl)
        {
            doHashtable = true;
            doCustomExcelReport = true;
        }
        
        public override string ReportName {
            get {
                return "Agmt_ContractDetails";
            }
           
        }

        public override Hashtable setParameterHTable(ref string rptName)
        {
            rptName = ReportName;
            reportTitle = "Contract Details";
            reportSource = ContractDetailsInstance.GetDataContainer();

            StringWriter sw = new StringWriter();
            rptSource.WriteXml(sw, XmlWriteMode.WriteSchema);
            sXMLData = sw.ToString();

            setReportDataSource(sXMLData);

            setReportUserParameter();

            return htParameter;
        }

        private ContractDetails ContractDetailsInstance
        {
            get
            {
                ContractDetails contractDetails = Policy.ResultsCache.GetObject(cachePolicy, ContractDetails.CacheKey) as ContractDetails;
                if (contractDetails == null)
                {
                    contractDetails = new ContractDetails();                   

                    contractDetails.Load(Utility.DataAccess.GetConnectionString(), userContext.DomainUserId, AgreementId, FundNumber);

                    Policy.ResultsCache.SetObject(cachePolicy, ContractDetails.CacheKey, contractDetails);
                }
                return contractDetails;
            }
        }

        private int AgreementId
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.ContractDetailsPageKey) as Hashtable;
                return (h == null || h[AgreementsIdkey] == null) ? 0 : (int)h[AgreementsIdkey];
            }

            set
            {
                if (AgreementId != value)
                {
                    Policy.ResultsCache.PurgeObject(cachePolicy);

                    Hashtable h = pageCachePolicy.GetCachedObject(Keys.ContractDetailsPageKey) as Hashtable;
                    if (h == null)
                    {
                        h = new Hashtable();
                    }

                    h[AgreementsIdkey] = value;
                    pageCachePolicy.Cache(Keys.ContractDetailsPageKey, h);
                }
            }
        }

        private string FundNumber
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.ContractDetailsPageKey) as Hashtable;
                return (h == null || h[FundNumberKey] == null) ? string.Empty : (string)h[FundNumberKey];
            }

            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.ContractDetailsPageKey) as Hashtable;
                if (h == null) h = new Hashtable();
                h[FundNumberKey] = value;
                pageCachePolicy.Cache(Keys.ContractDetailsPageKey, h);
            }
        }
    }
}
